package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 验证码Controller
 */
@RestController
@RequestMapping("/mobile/validateCode")
@Slf4j
public class MobileValidateCodeController {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     *  预约 时发送的验证码！
     * @param type
     * @param telephone
     * @return
     */
    @PostMapping("/send")
    public Result generateValidateCode(String type,String telephone){
        //生成验证码
        String validateCode = ValidateCodeUtils.generateValidateCode4String(4);
        //生成redis中存储的键
        String orderTelephone = type+"_"+telephone;
        //存入redis中
        redisTemplate.opsForValue().set(orderTelephone,validateCode,3,TimeUnit.MINUTES);
        log.info("手机号信息：{}，验证码：{}",orderTelephone,validateCode);
        //返回前端存入成功
        return new Result(true,MessageConst.SEND_VALIDATECODE_SUCCESS);
    }
//    @Autowired
//    private RedisTemplate redisTemplate;
//    //获取验证码，保存到redis数据库
//    @PostMapping("send")
//    public Result SendCode( @RequestParam("telephone") String phone,String type){
//        //生成验证码
//        Integer code = ValidateCodeUtils.generateValidateCode(4);
//        //存储到数据库
//        String newType = type+phone;
//        redisTemplate.opsForValue().set(newType, code,5,TimeUnit.MINUTES);
//        return new Result(true,MessageConst.ACTION_SUCCESS,null);
//    }


}
